<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="appid" prop="appid">
        <el-input
          v-model="queryParams.appid"
          placeholder="appid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="appsecret" prop="secret">
        <el-input
          v-model="queryParams.secret"
          placeholder="appsecret"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板id" prop="templateId">
        <el-input
          v-model="queryParams.templateId"
          placeholder="请输入模板id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="接收者id" prop="touser">
        <el-input
          v-model="queryParams.touser"
          placeholder="请输入接收者id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:wechat:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:wechat:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:wechat:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:wechat:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wechatList" @selection-change="handleSelectionChange">
        <el-table-column type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="appID">
            <span>{{ props.row.appid }}</span>
          </el-form-item>
           <el-form-item label="appsecret">
            <span>{{ props.row.secret }}</span>
          </el-form-item>
           <el-form-item label="模板id">
            <span>{{ props.row.templateId }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" show-overflow-tooltip/>
      <el-table-column label="appID" align="center" prop="appid" show-overflow-tooltip/>
      <el-table-column label="appsecret" align="center" prop="secret" show-overflow-tooltip/>
      <!-- <el-table-column label="数据json" align="center" prop="body" show-overflow-tooltip/> -->
      <el-table-column label="模板id" align="center" prop="templateId" show-overflow-tooltip/>
      <!-- <el-table-column label="接收者id" align="center" prop="touser" show-overflow-tooltip/> -->
      <!-- <el-table-column label="扩展字段" align="center" prop="v1" />
      <el-table-column label="扩展字段" align="center" prop="v2" />
      <el-table-column label="扩展字段" align="center" prop="v3" /> -->
      <el-table-column label="城市" align="center" prop="v4" /> 
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:wechat:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:wechat:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信公众号推送对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="appid" prop="appid">
              <el-input v-model="form.appid" placeholder="请输入appid" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="appsecret" prop="secret">
              <el-input v-model="form.secret" placeholder="请输入appsecret" />
            </el-form-item>
            </el-col>
            <!-- <el-col :span="24">
            <el-form-item label="数据json" prop="body">
              <el-input v-model="form.body" type="textarea" placeholder="请输入内容" />
            </el-form-item>
            </el-col> -->
            <el-col :span="12">
            <el-form-item label="模板id" prop="templateId">
              <el-input v-model="form.templateId" placeholder="请输入模板id" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="定时推送" prop="v2" :key="form.v2">
              <!-- <el-input v-model="form.v2" placeholder="请输入扩展字段" /> -->
              <el-time-select
              v-model="form.v2"
                :picker-options="{
                  start: '00:00',
                  step: '00:30',
                  end: '23:30'
                }"
                placeholder="选择时间">
              </el-time-select>
            </el-form-item>
            </el-col>
            <el-col :span="24">
            <el-form-item label="城市" prop="v4">
            <el-cascader
              placeholder="请选择城市"
              ref="cascaderArr"
                size="large"
                :options="options"
                v-model="selectedOptions"
                filterable
                clearable
                >
              </el-cascader>
            </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="扩展字段" prop="v8">
                <el-input v-model="form.v8" placeholder="请输入扩展字段" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="颜色选择" prop="v9" style=" height: 10px;">
                <el-color-picker v-model="form.v9" size="medium" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="开始时间" prop="v10">
                <el-date-picker clearable
                  v-model="form.v10"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择开始时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="扩展字段" prop="v11">
                <el-input v-model="form.v11" placeholder="请输入扩展字段" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="颜色选择" prop="v12" style=" height: 10px;">
                <el-color-picker v-model="form.v12" size="medium" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="开始时间" prop="v13">
                <el-date-picker clearable
                  v-model="form.v13"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择开始时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="扩展字段" prop="v14">
                <el-input v-model="form.v14" placeholder="请输入扩展字段" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="颜色选择" prop="v15" style=" height: 10px;">
                <el-color-picker v-model="form.v15" size="medium" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="开始时间" prop="v16">
                <el-date-picker clearable
                  v-model="form.v16"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择开始时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="扩展字段" prop="v17">
                <el-input v-model="form.v17" placeholder="请输入扩展字段" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="颜色选择" prop="v18" style=" height: 10px;">
                <el-color-picker v-model="form.v18" size="medium" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="开始时间" prop="v19">
                <el-date-picker clearable
                  v-model="form.v19"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择开始时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWechat, getWechat, delWechat, addWechat, updateWechat } from "@/api/system/wechat";
import { regionData } from 'element-china-area-data'
export default {
  name: "Wechat",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信公众号推送表格数据
      wechatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appid: null,
        secret: null,
        body: null,
        templateId: null,
        touser: null,
        v1: null,
        v2: null,
        v3: null,
        v4: null,
        v5: null,
        v6: null,
        v7: null,
        v8: null,
        v9: null,
        v10: null,
        v11: null,
        v12: null,
        v13: null,
        v14: null,
        v15: null,
        v16: null,
        v17: null,
        v18: null,
        v19: null,
        v20: null,
        v21: null,
        v22: null,
        v23: null,
        v24: null,
        v25: null,
        v26: null,
        v27: null,
        v28: null,
        v29: null,
        v30: null,
        v31: null,
        v32: null,
        v33: null,
        v34: null,
        v35: null,
        v36: null,
        v37: null,
        v38: null,
        v39: null,
        v40: null,
        createBy:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        appid: [
          { required: true, message: "第三方用户唯一凭证不能为空", trigger: "blur" }
        ],
        secret: [
          { required: true, message: "第三方用户唯一凭证密钥，即appsecret不能为空", trigger: "blur" }
        ],
        v4: [
          { required: true, message: "城市不能为空", trigger: "blur" }
        ],
        templateId: [
          { required: true, message: "模板id不能为空", trigger: "blur" }
        ],
        touser: [
          { required: true, message: "接收者id不能为空", trigger: "blur" }
        ],
      },
       options: regionData,
       selectedOptions: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询微信公众号推送列表 */
    getList() {
      this.loading = true;
      debugger
      const username = this.$store.state.user.name;
      if(username!=='admin'){
        this.queryParams.createBy = username
      }
      listWechat(this.queryParams).then(response => {
        this.wechatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        appid: null,
        secret: null,
        body: null,
        templateId: null,
        touser: null,
        v1: null,
        v2: null,
        v3: null,
        v4: null,
        v5: null,
        v6: null,
        v7: null,
        v8: null,
        v9: null,
        v10: null,
        v11: null,
        v12: null,
        v13: null,
        v14: null,
        v15: null,
        v16: null,
        v17: null,
        v18: null,
        v19: null,
        v20: null,
        v21: null,
        v22: null,
        v23: null,
        v24: null,
        v25: null,
        v26: null,
        v27: null,
        v28: null,
        v29: null,
        v30: null,
        v31: null,
        v32: null,
        v33: null,
        v34: null,
        v35: null,
        v36: null,
        v37: null,
        v38: null,
        v39: null,
        v40: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信公众号推送";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWechat(id).then(response => {
        this.form = response.data;
        debugger
        let selected1 = JSON.stringify(response.data.v3)
        let splitData = selected1.split(",");
        let cleanData = splitData.map(item => item.replace(/"/g, '')); // 移除双引号

        // //获取省份
        // selected1.substring(1, 7); 
        // //获取市
        // selected1.substring(8, 14);
        // console.log(selected1.substring(8, 14)) 
        // let arr = []
        // arr.push(selected1.substring(1, 7))
        // arr.push(selected1.substring(8, 14))
        // console.log(arr)
        this.selectedOptions = cleanData //重要，把处理后的数据重新赋值给selectOptions，让其显示
        this.open = true;
        this.title = "修改微信公众号推送";
      });
    },
    /** 提交按钮 */
    submitForm() {
      debugger
      const username = this.$store.state.user.name;
      const checkedNodes = this.selectedOptions
      console.log(checkedNodes)
      // this.editForm.province = CodeToText[value[0]] //得到省份
      const checkedNode =  this.$refs["cascaderArr"].getCheckedNodes();
      console.log( checkedNode[0].data.label ) ;  //获取当前点击节点的label值
      console.log(checkedNode[0].pathLabels[0])
      console.log( checkedNode[0].pathLabels) ;  //获取由label组成的数组
      let arr = []
      arr.push(checkedNodes[0])
      arr.push(checkedNodes[1])
      arr.push(checkedNodes[2])
      this.form.v3 = checkedNodes[0] + "," + checkedNodes[1] + "," + checkedNodes[2];
      this.form.v4 = checkedNode[0].pathLabels[0]
      if( this.form.createBy!==null){
        this.form.updateBy=username
      }else{
        this.form.createBy=username
      }
      this.form.updateTime =null
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWechat(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWechat(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微信公众号推送编号为"' + ids + '"的数据项？').then(function() {
        return delWechat(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wechat/export', {
        ...this.queryParams
      }, `wechat_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
